package com.lc.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lc.mapper.OmsOrderMapper;
import com.lc.pojo.OmsOrder;
import com.lc.pojo.UmsUser;
import com.lc.pojo.dto.OmsOrderPreSubmitDto;
import com.lc.pojo.dto.OmsOrderProductListDto;
import com.lc.pojo.dto.PmsSkuCartDto;
import com.lc.pojo.dto.SkuInfoDto;
import com.lc.pojo.param.SkuInfoParam;
import com.lc.service.OmsOrderService;
import com.lc.utils.ThreadVariablesUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 订单
 * @Author lc
 */

@Service
public class OmsOrderServiceImpl extends ServiceImpl<OmsOrderMapper, OmsOrder> implements OmsOrderService {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    //订单预支付信息
    @Override
    public OmsOrderPreSubmitDto getPreSubmit(SkuInfoParam skuInfos) {
        UmsUser umsUser = ThreadVariablesUtil.getThreadData();
        List<Object> listSKU = redisTemplate.opsForHash().values("ShopCar:" + umsUser.getAccount());

        OmsOrderPreSubmitDto submitDto = new OmsOrderPreSubmitDto();
        List<OmsOrderProductListDto> productList = new ArrayList<>();

        List<SkuInfoDto> infos = skuInfos.getSkuInfos();

        //订单总金额合计
        BigDecimal totalMoney = listSKU.stream()
                .map(sku -> JSONUtil.parseObj(sku).toBean(PmsSkuCartDto.class))
                .peek(beanSKU -> {
                    List<SkuInfoDto> infos1 = skuInfos.getSkuInfos();
                    infos1.stream()
                            .filter(in -> in.getSkuId().equals(beanSKU.getSkuId()))
                            .forEach(info -> {
                                OmsOrderProductListDto listDto = new OmsOrderProductListDto();
                                listDto.setId(Math.toIntExact(beanSKU.getSkuId()));
                                BeanUtils.copyProperties(beanSKU, listDto);
                                productList.add(listDto);
                            });
                })
                .map(beanSKU ->
                        infos.stream()
                                .filter(info -> info.getSkuId().equals(beanSKU.getSkuId()))
                                .map(info -> new BigDecimal(info.getSkuNum()).multiply(beanSKU.getUnitPrice()))
                                .reduce(BigDecimal.ZERO, BigDecimal::add)
                )
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        submitDto.setTotalMoney(totalMoney);


        // 设置产品列表
        submitDto.setProductList(productList);

        // 设置运费
        submitDto.setFreight(BigDecimal.valueOf(0));

        // 计算实付金额合计
        BigDecimal payMoney = submitDto.getTotalMoney().add(submitDto.getFreight());
        submitDto.setPayMoney(payMoney);

        return submitDto;
    }

}
